<?php

namespace App\Http\Controllers\storage;

use App\Http\Controllers\Controller;

class WarehouseController extends Controller
{
    public function index()
    {
        $menu_id = request('curr_menu_id', 0);
        if(!$menu_id){
            return response()->json('非法操作');
        }
        $toolBarButton = $this->buildToolBarButton($menu_id);
        return view('storage.warehouse.list',compact('menu_id','toolBarButton'));
    }

    public function listData()
    {
        $params['curr_menu_id'] = request('curr_menu_id', 0);
        if(!$params['curr_menu_id']){
            return response()->json(['code'=>400, '页面无菜单ID']);
        }
        $params['page'] = request('page', 1);
        $params['rows'] = request('rows', 10);
        try{
            $list = callApiServer("Storage.WarehouseService.getInfoList", $params);
            return response()->json($list);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }


    public function warehouseList(){

        $list = callApiServer("Storage.WarehouseService.getInfoList", []);
        return response()->json($list);
    }

    public function showAddForm()
    {
        return view('storage.warehouse.form');
    }

    public function showEditForm()
    {
        $params['id'] = request('warehouse_id');
        try{
            $info = callApiServer("Storage.WarehouseService.getInfoById", $params);
            if(empty($info)){
                return response()->json('对应数据不存在');
            }
            return view('storage.warehouse.form', compact("info"));
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>500,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function save()
    {
        $params['id'] = request('id',0);
        $params['name'] = request('name');
        $params['province_id'] = request('province',0);
        $params['city_id'] = request('city',0);
        $params['area_id'] = request('area',0);
        $params['area_info'] = request('area_info',0);
        $params['address'] = request('address');
        $params['contact'] = request('contact');
        $params['contact_way'] = request('contact_way');
        if(empty($params['name'])){
            return response()->json(['code'=>400,'data'=>'仓库名称不能为空']);
        }
        if(empty($params['address'])){
            return response()->json(['code'=>400,'data'=>'详细地址不能为空']);
        }
        if(empty($params['contact'])){
            return response()->json(['code'=>400,'data'=>'联系人不能为空']);
        }
        if(empty($params['contact_way'])){
            return response()->json(['code'=>400,'data'=>'联系方式不能为空']);
        }
        $rs = callApiServer("Storage.WarehouseService.saveData", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>$rs]);
    }
}
